<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['supplier'][$_SESSION['s_userType'.$cfgPack]])
      && $cfgFileNames['supplier'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $partyItemDetailArray = array();
  $branchArray          = array();
  $companyName          = "";
  $contactPerson1       = "";
  $contactPerson2       = "";
  $phoneNo              = "";
  $address              = "";
  $mobile1              = "";
  $mobile2              = "";
  $city                 = "";
  $email                = "";
  $web                  = "";
  $i                    = 0;  
  $supplierId           = isset($_REQUEST['supplierId']) ? $_REQUEST['supplierId'] : 0;
  $categoryId           = 0;
  $challanShort         = isset($_REQUEST['challanShort']) ? $_REQUEST['challanShort'] : "";
  $redirectFrom         = isset($_REQUEST['redirectFrom']) ? $_REQUEST['redirectFrom'] : "";  
  $lastInserId          = 0;

  if(isset($_POST['cancelBtn']))
  {
    header("Location:supplier.php");
    exit();
  }
  // after post : start 
  if(isset($_POST['companyName']))
  {
    if($supplierId > 0)
    {
      $updateParty = "UPDATE supplier SET companyName = '".$_POST['companyName']."',
                                       contactPerson1 = '".$_POST['contactPerson1']."',
                                       contactPerson2 = '".$_POST['contactPerson2']."',
                                       phoneNo        = '".$_POST['phoneNo']."',
                                       address        = '".$_POST['address']."',
                                       mobile1        = '".$_POST['mobile1']."',
                                       mobile2        = '".$_POST['mobile2']."',
                                       email          = '".$_POST['email']."',
                                       web            = '".$_POST['web']."'
                                 WHERE supplierId     = '".$_REQUEST['supplierId']."'";
      $updatePartyRes   = mysql_Query($updateParty);
      if(!$updatePartyRes)
        die("Insert Query Not Inserted : ".mysql_error());
      else
        header("Location:supplier.php");
    }
    else
    {
      $address = addslashes($_POST['address']);
      $phoneNo = $_POST['code']." ".$_POST['phoneNo'];
      $insertParty = "INSERT INTO supplier (companyName, contactPerson1, contactPerson2, phoneNo, address, mobile1, mobile2, city, email, web, partyTINNo)
                            VALUE ('".$_POST['companyName']."','".$_POST['contactPerson1']."','".$_POST['contactPerson2']."','".$phoneNo."',
                                   '".$address."','".$_POST['mobile1']."','".$_POST['mobile2']."','".$_POST['city']."','".$_POST['email']."',
                                   '".$_POST['web']."','".$_POST['partyTINNo']."')";
                                   
      $insertPartyResult = mysql_query($insertParty);
      $lastInserId = mysql_insert_id();
      $loopCount = 0;
      while($loopCount < count($_POST['subCategoryId']))
      {
        $categoryId    = ($_POST['categoryId'] != '') ? $_POST['categoryId'] : 0;
        $subcategoryId = ($_POST['subCategoryId'][$loopCount] != '') ? $_POST['subCategoryId'][$loopCount] : 0;
        if($_POST['categoryId'] != "" || $_POST['subCategoryId'][$loopCount] != "")
        {
         $insertitemsupplier = "INSERT INTO itemsupplier (supplierId,categoryId,subCategoryId)
                                     VALUES (".$supplierId.",".$categoryId .",".$subcategoryId.")";
         $insertitemsupplierQuery = mysql_query($insertitemsupplier);
        }
        $loopCount++;
      }
      if(!$insertPartyResult)
        die("Insert Query Not Inserted : ".mysql_error());
        
      if($redirectFrom != 'deliveryChalan')
      {
        header("Location:supplier.php");
      }  
      else
      {
        header("Location:deliveryChalan.php?lastInserId=".$lastInserId."&challanShort=GIN".$challanShort);        
      }      
    }
  }    
  // after post : end
  
  ////Select category : Start
  $categoryIdArr    = array();
	$categoryNameArr  = array();
	$categorySelected = (isset($_REQUEST['categoryId']) && $_REQUEST['categoryId'] != "") ? $_REQUEST['categoryId'] : "";
  
	$selectCategoryQuery = "SELECT categoryId,categoryName
	                          FROM category
	                      ORDER BY categoryName";
	$selectCategoryQueryRes = mysql_query($selectCategoryQuery);
	$j = 0;
	while($categoryRow = mysql_fetch_array($selectCategoryQueryRes))
	{
	  if($j == 0 || (isset($_REQUEST['categoryId']) && $_REQUEST['categoryId'] == $categoryRow['categoryId']))
	  $categorySelected      = $categoryRow['categoryId'];
	  $categoryIdArr[$j]     = $categoryRow['categoryId'];
	  $categoryNameArr[$j]   = $categoryRow['categoryName'];
	  $j++;
	}
	////Select category : End
	
  $selectItemIn = "SELECT supplier.supplierId,supplier.companyName,supplier.contactPerson1,supplier.contactPerson2,supplier.phoneNo,
                          supplier.address,supplier.mobile1,supplier.mobile2,supplier.city,supplier.email,supplier.web
                     FROM supplier
                 ORDER BY companyName";
  $selectItemInResult = mysql_query($selectItemIn);
  $i = 0;
  while($supplierRow = mysql_fetch_array($selectItemInResult))
  {
    $partyItemDetailArray[$i]['supplierId']     = $supplierRow['supplierId'];
    $partyItemDetailArray[$i]['companyName']    = $supplierRow['companyName'];
    $partyItemDetailArray[$i]['contactPerson1'] = $supplierRow['contactPerson1'];
    $partyItemDetailArray[$i]['contactPerson2'] = $supplierRow['contactPerson2'];
    $partyItemDetailArray[$i]['phoneNo']        = $supplierRow['phoneNo'];
    $partyItemDetailArray[$i]['address']        = $supplierRow['address'];
    $partyItemDetailArray[$i]['mobile1']        = $supplierRow['mobile1'];
    $partyItemDetailArray[$i]['mobile2']        = $supplierRow['mobile2'];
    $partyItemDetailArray[$i]['city']           = $supplierRow['city'];
    $partyItemDetailArray[$i]['email']          = $supplierRow['email'];
    $partyItemDetailArray[$i]['web']            = $supplierRow['web'];
    
    if($supplierRow['supplierId'] == $supplierId)
    {
      $sql = "SELECT itemsupplier.categoryId,itemsupplier.supplierId,itemsupplier.subCategoryId 
                FROM  itemsupplier 
                JOIN  category  ON category.categoryId = itemsupplier.categoryId
               WHERE supplierId =".$supplierId;
      $selRes = mysql_query($sql);
      if($row = mysql_fetch_array($selRes))
      {
        $categorySelected      = $row['categoryId'];  
      }
      $companyName    = $supplierRow['companyName'];  
      $contactPerson1 = $supplierRow['contactPerson1'];  
      $contactPerson2 = $supplierRow['contactPerson2'];  
      $phoneNo        = $supplierRow['phoneNo'];  
      $address        = $supplierRow['address'];  
      $mobile1        = $supplierRow['mobile1'];  
      $mobile2        = $supplierRow['mobile2'];  
      $city           = $supplierRow['city'];  
      $email          = $supplierRow['email'];  
      $web            = $supplierRow['web'];  
    }
    $i++;     
  }
  
   
  include("./bottom.php");
  $smarty->assign("supplierId",$supplierId);
  $smarty->assign("challanShort",$challanShort);
  $smarty->assign("companyName",$companyName);
  $smarty->assign("contactPerson1",$contactPerson1);
  $smarty->assign("contactPerson2",$contactPerson2);
  $smarty->assign("phoneNo",$phoneNo);
  $smarty->assign("address",$address);
  $smarty->assign("mobile1",$mobile1);
  $smarty->assign("mobile2",$mobile2);
  $smarty->assign("city",$city);
  $smarty->assign("email",$email);
  $smarty->assign("web",$web);
  $smarty->assign("categorySelected",$categorySelected);
  $smarty->assign("categoryIdArr",$categoryIdArr);
  $smarty->assign("categoryId",$categoryId);
  $smarty->assign("categoryNameArr",$categoryNameArr);
  $smarty->assign("partyItemDetailArray",$partyItemDetailArray);
  $smarty->assign("lastInserId",$lastInserId);
  $smarty->assign("redirectFrom",$redirectFrom);
  $smarty->display("supplier.tpl");
}
?>